package com.easy.app.controller.system;

import com.easy.framework.common.model.R;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.entity.SysOperLog;
import com.easy.module.log.enums.Operation;
import com.easy.module.log.query.SysOperLogQuery;
import com.easy.module.log.service.ISysOperLogService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 操作日志记录
 *
 * @author Easy
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/log/oper")
public class SysOperlogController extends BaseController {
    private final ISysOperLogService sysOperLogService;

    @PreAuthorize("@perm.hasPermission('system:oper:log$:list')")
    @GetMapping("/page")
    public R<Pagination<SysOperLog>> page(SysOperLogQuery query) {
        return R.success(sysOperLogService.page(query, () -> sysOperLogService.queryList(query)));
    }

    @Log(title = "操作日志", type = Operation.EXPORT)
    @PreAuthorize("@perm.hasPermission('system:oper:log$:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLogQuery query) throws IOException {
        List<SysOperLog> list = sysOperLogService.queryList(query);
        ExcelUtils.exportExcel(response, list, "操作日志");
    }

    @Log(title = "操作日志", type = Operation.DELETE)
    @PreAuthorize("@perm.hasPermission('system:oper:log$:remove')")
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable("ids") Long[] ids) {
        return R.result(sysOperLogService.removeByIds(List.of(ids)));
    }

    @Log(title = "操作日志", type = Operation.CLEAN)
    @PreAuthorize("@perm.hasPermission('system:oper:log$:remove')")
    @DeleteMapping("/clean")
    public R clean() {
        sysOperLogService.clean();
        return R.success();
    }
}
